<template>
<!--  用户信息表-->
  <div style="margin-left: 5rem">
    <el-text>用户信息:</el-text>
    <el-button type="primary" size="small" style="float: right" @click="changeEvent=true">新增用户</el-button>
    <el-table
        :data="this.tableData"
        style="width: 45rem"
        height="250">
      <el-table-column
          prop="account"
          label="用户账号"
          width="120">
      </el-table-column>
      <el-table-column
          fixed
          prop="userName"
          label="用户名称"
          width="150">
      </el-table-column>
      <el-table-column
          prop="userRole"
          label="用户角色"
          width="120">
      </el-table-column>
      <el-table-column prop="operate" label="操作" width="auto">
        <template v-slot="scope">
          <el-button type="primary" size="small" @click="editData(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="small" @click="deleteData(scope.row.id)">删除</el-button>
          <el-button type="danger" size="small" @click="changePwd(scope.row.id)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!--操作用户弹窗-->
  <el-dialog title="操作用户信息" style="width: 30rem;" v-model="this.changeEvent" :close-on-click-modal="false"
             :show-close="false">
    <el-form ref="rowData" :model="this.rowData" label-width="80px" style="margin-left: 3rem">
      <!--v-model双向绑定数据-->
      <!-- 1rem===16px-->
      <el-form-item label="用户账号">
        <el-input
            style="width: 15rem;"
            v-model="this.rowData.account"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户名称">
        <el-input
            style="width: 15rem;"
            v-model="this.rowData.userName"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户角色">
        <el-select
            v-model="this.rowData.userRole"
            placeholder="请选择角色"
            style="width: 15rem;"
        >
          <el-option
              v-for="(type,item) in this.roleOptions"
              :key="type"
              :label="type"
              :value="item"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="this.submitRowData()">提交</el-button>
        <el-button @click="this.closeDialog()">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import request from "@/utils/request.js";

export default {
  name: "User",
  data() {
    return {
      // 弹出框
      changeEvent: false,
      // 表格所有数据
      tableData: [],
      // 编辑行数据
      rowData: {},
      // 角色数据
      roleOptions:{},
    }
  },
  created() {
    // this.getTableData();
    // this.getRoleOptions();
  },
  methods: {
    // 获取所有用户
    getTableData() {
      //给后端接口发送请求
      request.get("/user/all").then(res => {
        if (res.data.code === '200') {
          this.tableData = res.data.data;
        }
      })
    },
    //新增或者编辑航班数据
    editData(id) {
      // 获取行数据
      request.get("/user/" + id).then(res => {
        if (res.data.code === '200') {
          this.rowData = res.data.data;
        }
      })
      // 显示弹窗
      this.changeEvent = true;
    },
    // 删除航班数据
    deleteData(id) {
      request.get("/user/delete/" + id).then(res => {
        if (res.data.code === '200') {
          this.success();
        }
      })
    },
    //重置用户密码
    changePwd(id) {
      request.get("/user/resetPwd/"+id).then(res=>{
        if (res.data.code==='200'){
          this.success();
        }
      })
    },
    // 提交行数据
    submitRowData() {
      request.post("/user/edit", this.rowData).then(res => {
        if (res.data.code === '200') {
          this.success()
        }
      })
    },
    // 获取角色列表
    getRoleOptions(){
      request.get("/role/roleMap").then(res=>{
        if (res.data.code==='200'){
          this.roleOptions=res.data.data;
        }
      })
    },
    closeDialog() {
      // 关闭弹窗
      this.changeEvent = false;
      // 清空数据
      this.rowData = {};
    },
    success(){
      //关闭弹窗
      this.changeEvent = false;
      // 重新获取表格数据
      this.getTableData();
      // 清空提交信息
      this.rowData = {};
      // 显示提交成功
      this.$message.success("操作成功")
    }
  }
}

</script>


<style>

</style>